﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using WebSocketChat.Web.Models;
using Newtonsoft.Json;

namespace WebSocketChat.Web.Controllers
{
    /// <summary>
    /// webAPI 仅供PC客户端调用
    /// </summary>
    public class LoginController : ApiController
    {
        private WebSocketChatEntities dbContext;

        public WebSocketChatEntities DbContext
        {
            get
            {
                if (dbContext == null)
                    dbContext = new WebSocketChatEntities();
                return dbContext;
            }
        }

        // GET api/values
        public IEnumerable<ChatUser> Get()
        {
            return DbContext.ChatUser.ToList();
        }

        // GET api/values/5
        public ChatUser Get(int id)
        {
            return DbContext.ChatUser.FirstOrDefault(t => t.ID == id);
        }

        // POST api/values
        public ResultObject<ChatUser> Post([FromBody]ChatUser user)
        {
            var euser = DbContext.ChatUser.FirstOrDefault(t => t.LoginID == user.LoginID && t.PassWords == user.PassWords);
            ResultObject<ChatUser> res = new ResultObject<ChatUser>()
            {
                code = 200,
                count = 1,
                msg = "success"
            };
            if (euser != null)
            {
                res.data = euser;
            }
            else
            {
                res.code = 404;
                res.msg = "user not exists";
            }
            return res;
        }

        // PUT api/values/5
        public void Put(int id, [FromBody]string value)
        {
        }

        // DELETE api/values/5
        public void Delete(int id)
        {
        }
    }

    public class ResultObject<T> where T : class
    {
        public int code { get; set; }
        public string msg { get; set; }
        public int count { get; set; }
        public T data { get; set; }
    }

    public class ResultObject
    {
        public int code { get; set; }
        public string msg { get; set; }
        public int count { get; set; }
        public object data { get; set; }
    }

    public class UploadFileInfo
    {
        public string src { get; set; }
        public string name { get; set; }
    }
}
